var merchant = {
    currentProductPageNo:0,
    
    getCurrentUserInfo:function(flag){
        $.ajax({
            type: "GET",
            url: config.baseURL + "account/userInfo.json",
            cache:false,
            dataType: "json",
            success: function(data){
                if(data.result == 'success'){
                    var account = data.account;
                    if(flag==0){
                        merchant.showUserInfo(account);
                    }else{
                        merchant.setUserInfoToUpdate(account);
                    }
                }else{
                	$.msgBox.window({
                        title:"提示",
                        msg : "<span>获取个人信息失败！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                }
            }
        });
    },
    showUserInfo:function(account){
        $('#nickName').html(account.nickName);
        $('#userName').html(account.userName);
        $('#lastLoginDateTime').html(new Date(account.lastLoginDate).pattern("yyyy-MM-dd HH:mm:ss"));
    },
    setUserInfoToUpdate:function(account){
        $('#id').val(account.id);
        
        if(account.telephone){
            var tel = account.telephone;
            var telArray = tel.split("-");
            $("#zoneNo").val(telArray[0]?telArray[0]:"");
            $("#telNo").val(telArray[1]?telArray[1]:"");
            $("#extensionNo").val(telArray[2]?telArray[2]:"");
            
            $("#tel").val(tel);
        }
        if(account.province){
            utils.getProvince(account.province.id);
            if(account.province.provinceId){
                utils.getCity(account.province.provinceId,account.city.id);
                utils.getArea(account.city.cityId,account.area.id);
            }
        }else{
            utils.getProvince(null);
        }
        
        $('#userName').html(account.userName);
        $('#nickName').val(account.nickName);
        $('#merchantName').val(account.merchantName);
    },
    addProduct:function(createMode){
        var productName = $('#productName').val();
        var discountPrice = $('#discountPrice').val();
        var originalPrice = $('#originalPrice').val();
        var type = $('#type').val();
        var hot = $(':radio[name=hot]:checked').val();
        var stock = $('#stock').val();
        var author = $('#author').val();
        var publishingCompany = $('#publishingCompany').val();
        var publishingDate = $('#publishingDate').val();
        var imagePath = $('#imagePath').val();
        
        if(!productName){
            $.msgBox.window({
                title:"提示",
                msg : "<span>请输入商品名称！</span>",
                icon:"succeed",
                time:1500,
                width:250,
                cls:'miniborder'
            });
            return false;
        }
        if(!discountPrice){
            $.msgBox.window({
                title:"提示",
                msg : "<span>请输入商品优惠价格！</span>",
                icon:"succeed",
                time:1500,
                width:250,
                cls:'miniborder'
            });
            return false;
        }
        if(!originalPrice){
            $.msgBox.window({
                title:"提示",
                msg : "<span>请输入商品定价！</span>",
                icon:"succeed",
                time:1500,
                width:250,
                cls:'miniborder'
            });
            return false;
        }
        if(type==0){
            $.msgBox.window({
                title:"提示",
                msg : "<span>请选择商品分类！</span>",
                icon:"succeed",
                time:1500,
                width:250,
                cls:'miniborder'
            });
            return false;
        }
        if(!stock){
            $.msgBox.window({
                title:"提示",
                msg : "<span>请输入商品库存！</span>",
                icon:"succeed",
                time:1500,
                width:250,
                cls:'miniborder'
            });
            return false;
        }
        if(!author){
            $.msgBox.window({
                title:"提示",
                msg : "<span>请输入作者！</span>",
                icon:"succeed",
                time:1500,
                width:250,
                cls:'miniborder'
            });
            return false;
        }
        if(!publishingCompany){
            $.msgBox.window({
                title:"提示",
                msg : "<span>请输入出版社！</span>",
                icon:"succeed",
                time:1500,
                width:250,
                cls:'miniborder'
            });
            return false;
        }
        if(!publishingDate){
            $.msgBox.window({
                title:"提示",
                msg : "<span>请输入出版时间！</span>",
                icon:"succeed",
                time:1500,
                width:250,
                cls:'miniborder'
            });
            return false;
        }
        if(!imagePath){
            $.msgBox.window({
                title:"提示",
                msg : "<span>请选择商品相片！</span>",
                icon:"succeed",
                time:1500,
                width:250,
                cls:'miniborder'
            });
            return false;
        }
        
        $.ajax({
            type: "POST",
            url: config.baseURL + "product/"+createMode+".json",
            cache:false,
            dataType: "json",
            data : {
                        "id":$('#productId').val(),
                        "productName": productName,
                        "discountPrice" : discountPrice,
                        "originalPrice" : originalPrice,
                        "type" : type,
                        "hot" : hot,
                        "stock" : stock,
                        "author" : author,
                        "publishingCompany" : publishingCompany,
                        "publishingDate" : publishingDate,
                        "imagePath" : imagePath
                    },
            success: function(data){
                if(data.result == 'success'){
                	if(createMode=='create'){
                        $('#productListLink').click();
                	}else{
                        $.msgBox.window({
                            title:"提示",
                            msg : "<span>商品修改成功！</span>",
                            icon:"succeed",
                            time:1500,
                            width:250,
                            cls:'miniborder'
                        });
                	}
                }else{
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>产品操作失败！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                }
            }
        });
    },
    productList:function(page){
    	var perPageSize = 10;
        $.ajax({
            type: "GET",
            url: config.baseURL + "product/list.json",
            cache:false,
            dataType: "json",
            data : {"currentPage":page + 1,"perPageSize":perPageSize},
            success: function(data){
                if(data.result == 'success'){
                    $('#productList').html("");

                    $.each(data.productList, function(index, obj) {
                    	var hot = obj.hot==0?"否":"是";
                        var html = '<tr><td>'+(page*perPageSize+index+1)+'</td><td><a href="#" >'+obj.productName+'</a></td><td>动漫</td>' +
                        		'<td>￥'+obj.originalPrice+'</td><td>￥'+obj.discountPrice+'</td><td>'+obj.stock+'</td><td>'+hot+'</td>'+
                                '<td><a href="javascript:void(0);" onclick="merchant.toEditProductPage('+obj.id+')" class="miniGrayBtn">编辑</a>&nbsp;&nbsp;' +
                                '<a href="javascript:void(0);" onclick="merchant.deleteProduct('+obj.id+')" class="grayShortBtn">删除</a></td></tr>';
                            
                        $('#productList').append(html);
                    });

                    $("#paginationDiv").pagination(data.productListCount, {
                        callback: merchant.productList,//回调函数
                        num_display_entries: 5,
                        num_edge_entries: 3,
                        items_per_page:perPageSize,//每页显示多少条
                        current_page:page//当前页码
                    });
                    
                    merchant.currentProductPageNo = page;
                }else{
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>查询异常，请稍候再试！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                }
            }
        });
    },
    toEditProductPage:function(productId){
        $('#mainContent').load('editProduct.jsp?productId='+productId);
    },
    loadProductInfoToModify:function(productId){
    	$.ajax({
            type: "GET",
            url: config.baseURL + "product/detail.json",
            cache:false,
            dataType: "json",
            data : {"id":productId},
            success: function(data){
                if(data.result == 'success'){
                    var obj = data.product;
                    $('#productId').val(obj.id);
                    $('#productName').val(obj.productName);
                    $('#discountPrice').val(obj.discountPrice);
                    $('#originalPrice').val(obj.originalPrice);
                    $('#stock').val(obj.stock);
                    $('#author').val(obj.author);
                    $('#publishingCompany').val(obj.publishingCompany);
                    $('#publishingDate').val(obj.publishingDate);
                    $('#productImagePreview').attr('src',config.baseURL + obj.imagePath);
                    $('#imagePath').val(obj.imagePath);
                    
                    $('#type').val(obj.type);
                    $("input[type=radio][value="+obj.hot+"]").attr("checked",'checked'); 
                }else{
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>查询异常，请稍候再试！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                }
            }
        });
    },
    deleteProduct:function(id){
        $.ajax({
            type: "POST",
            url: config.baseURL + "product/delete.json",
            cache:false,
            dataType: "json",
            data : {"id":id},
            success: function(data){
                if(data.result == "success"){
                    merchant.productList(merchant.currentProductPageNo);
                }else{
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>删除异常，请稍候再试！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                }
            }
       });
    },
    orderList:function(page){
    	var perPageSize = 10;
    	$.ajax({
            type: "GET",
            url: config.baseURL + "order/list.json",
            cache:false,
            dataType: "json",
            data : {"currentPage":page + 1,"perPageSize":perPageSize,"orderType":"desc"},
            success: function(data){
                if(data.result == 'success'){
                    $('#orderList').html("");
        				
                    $.each(data.orderList, function(index, obj) {
                    	var status = "已发货";
                    	var statusButton = '<a href="javascript:void(0);" class="miniLightGrayBtn">已发货</a>';
                    	if(obj.status==0){
                    		status = "未发货";
                    		statusButton = '<a href="javascript:void(0);" onclick="merchant.changeOrderStatus('+obj.id+');" class="miniGrayBtn">已发货</a>';
                    	}

                        var html = '<tr><td>'+(page*perPageSize+index+1)+'</td><td><a href="javascript:void(0);">'+obj.id+'</a></td>'+
                                '<td>'+obj.name+'</td><td>货到付款</td><td>￥'+obj.product.discountPrice*obj.count+'</td>'+
                                '<td>'+status+'</td><td>'+new Date(obj.createDate).pattern("yyyy-MM-dd")+'</td>'+
                                '<td>'+statusButton+'&nbsp;&nbsp;'+
                                '<a href="javascript:void(0)" onclick="merchant.deleteOrder('+obj.id+');" class="grayShortBtn">删除</a></td>'+
                                '</tr>';
                            
                        $('#orderList').append(html);
                    });
                    
                    $("#paginationDiv").pagination(data.orderListCount, {
                        callback: merchant.orderList,//回调函数
                        num_display_entries: 5,
                        num_edge_entries: 3,
                        items_per_page:perPageSize,//每页显示多少条
                        current_page:page//当前页码
                    });
                    
                    merchant.currentOrderPageNo = page;
                }else{
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>查询异常，请稍候再试！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                }
            }
        });
    },
    changeOrderStatus:function(orderId){
    	$.ajax({
            type: "POST",
            url: config.baseURL + "order/update.json",
            cache:false,
            dataType: "json",
            data : {"id":orderId},
            success: function(data){
                if(data.result == "success"){
                    merchant.orderList(merchant.currentProductPageNo);
                }else{
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>更新状态失败，请稍候再试！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                }
            }
       });
    },
    deleteOrder:function(orderId){
        $.ajax({
            type: "POST",
            url: config.baseURL + "order/delete.json",
            cache:false,
            dataType: "json",
            data : {"id":orderId},
            success: function(data){
                if(data.result == "success"){
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>删除成功！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                    merchant.orderList(merchant.currentProductPageNo);
                }else{
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>删除异常，请稍候再试！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                }
            }
       });
    },
    initMerchantInfoForm:function(){
        var options = {
            dataType : 'json',
            beforeSerialize : function(form, options) {
                var zoneNo = $("#zoneNo").val();
                var telNo = $("#telNo").val();
                var extensionNo= $("#extensionNo").val();
                $('#tel').val(zoneNo+"-"+telNo+"-"+extensionNo);
                
                options.url = config.baseURL + 'account/update.json';
            },
            beforeSubmit : function() {
                if(!$('#merchantName').val()){
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>请输入商户名称！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                    return false;
                }
                if($('#province').val()==0){
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>请选择省份！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                    return false;
                }
                if($('#city').val()==0){
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>请选择城市！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                    return false;
                }
                if($('#area').val()==0){
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>请选择地区！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                    return false;
                }
                if(!$('#zoneNo').val()){
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>请输入固定电话区号！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                    return false;
                }
                if(!$('#telNo').val()){
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>请输入固定电话号码！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                    return false;
                }
                $('#merchantInfoForm').attr('action',config.baseURL + 'account/update.json');
                return true;
            },
            success : function(data) {
                if(data.result=='success'){
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>资料更新成功！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                }else{
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>资料更新失败，请稍候再试！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                }
            }
        };
        
        $('#merchantInfoForm').submit(function() {
            $(this).ajaxSubmit(options);
            return false;
        });
    },
    initCustomerServiceInfoForm:function(){
        var options = {
            dataType : 'json',
            beforeSerialize : function(form, options) {
                var zoneNo = $("#zoneNo").val();
                var telNo = $("#telNo").val();
                var extensionNo= $("#extensionNo").val();
                $('#tel').val(zoneNo+"-"+telNo+"-"+extensionNo);
                
                options.url = config.baseURL + 'account/update.json';
            },
            beforeSubmit : function() {
                if(!$('#nickName').val()){
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>请输入客服名称！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                    return false;
                }
                if($('#province').val()==0){
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>请选择省份！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                    return false;
                }
                if($('#city').val()==0){
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>请选择城市！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                    return false;
                }
                if($('#area').val()==0){
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>请选择地区！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                    return false;
                }
                if(!$('#zoneNo').val()){
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>请输入固定电话区号！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                    return false;
                }
                if(!$('#telNo').val()){
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>请输入固定电话号码！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                    return false;
                }
                $('#customerInfoForm').attr('action',config.baseURL + 'account/update.json');
                return true;
            },
            success : function(data) {
                if(data.result=='success'){
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>资料更新成功！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                }else{
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>资料更新失败，请稍候再试！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                }
            }
        };
        
        $('#customerInfoForm').submit(function() {
            $(this).ajaxSubmit(options);
            return false;
        });
    },
    createCustomerService:function(){
    	var userName = $('#userName').val();
    	if(userName){
            $.ajax({
                type: "POST",
                url: config.baseURL + "account/createCustomerServiceAccount.json",
                cache:false,
                dataType: "json",
                data : {"userName":userName},
                success: function(data){
                    if(data.result == 'success'){
                        $.msgBox.window({
                            title:"提示",
                            msg : "<span>创建客服成功！</span>",
                            icon:"succeed",
                            time:1500,
                            width:250,
                            cls:'miniborder'
                        });
                        $('#userName').val('');
                        merchant.queryCustomerServiceList();
                    }else{
                        $.msgBox.window({
                            title:"提示",
                            msg : "<span>创建客服异常，请稍候再试！</span>",
                            icon:"succeed",
                            time:1500,
                            width:250,
                            cls:'miniborder'
                        });
                    }
                }
            });
    	}else{
            $.msgBox.window({
                title:"提示",
                msg : "<span>请输入客服帐号！</span>",
                icon:"succeed",
                time:1500,
                width:250,
                cls:'miniborder'
            });
    	}
    },
    queryCustomerServiceList:function(){
        $.ajax({
            type: "GET",
            url: config.baseURL + "account/queryCustomerServiceAccountList.json",
            cache:false,
            dataType: "json",
            data : {"accountType":3},
            success: function(data){
                if(data.result == 'success'){
                    $('#accountList').html("");
                        
                    $.each(data.accountList, function(index, obj) {
                    	var onlineStatus = obj.onlineStatus==0?'否':'是';
                        var html = '<tr><td>'+(index+1)+'</td><td>'+obj.userName+'</td><td>'+onlineStatus+'</td>'+
                                '<td><a href="javascript:void(0);" onclick="merchant.deleteCustomerServiceList('+obj.id+');" class="grayShortBtn">删除</a></td></tr>';
                            
                        $('#accountList').append(html);
                    });
                }else{
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>查询异常，请稍候再试！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                }
            }
        });
    },
    deleteCustomerServiceList:function(accountId){
        $.ajax({
            type: "POST",
            url: config.baseURL + "account/deleteCustomerServiceAccount.json",
            cache:false,
            dataType: "json",
            data : {"id":accountId},
            success: function(data){
                    if(data.result == 'success'){
                        $.msgBox.window({
                            title:"提示",
                            msg : "<span>删除成功！</span>",
                            icon:"succeed",
                            time:1500,
                            width:250,
                            cls:'miniborder'
                        });
                        merchant.queryCustomerServiceList();
                    }else{
                        $.msgBox.window({
                            title:"提示",
                            msg : "<span>删除异常，请稍候再试！</span>",
                            icon:"succeed",
                            time:1500,
                            width:250,
                            cls:'miniborder'
                        });
                    }
            }
        });
    }
};